/**
 * @Author: 焦质晔
 * @Date: 2019/6/20
 * @Last Modified by: 杨光宇
 * @Last Modified time: 2021-02-23 17:29:50
 */
import {
  isFormEmpty
} from './index';

// 注意：表单控件如果使用了自定义校验规则，
// rules 配置中不能加 message 属性，required 的非空校验也需要在自定义的校验方法中实现

export const phone = (rule, value, callback) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var pattern = /^1[2-9]\d{9}$/;
  if (pattern.test(value)) {
    callback();
  }
  callback(new Error('请输入正确的手机号码'));
};
//手机号固化
export const phoneAndSolidify = (rule, value, callback) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var pattern = /^1[3-9]\d{9}$/;
  var solidify = /^(0\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,6})?$/;
  if (pattern.test(value) || solidify.test(value)) {
    callback();
  }
  callback(new Error('请输入正确的手机号或固话'));
};

// 可传提示信息手机号校验
export const phoneCus = (rule, value, callback, message) => {
  if (rule.required && isFormEmpty(value)) {
    return callback(new Error('请输入' + message));
  }
  let regExp = /^1[2-9]\d{9}$/;
  if (isFormEmpty(value) || regExp.test(value)) {
    return callback();
  }
  callback(new Error('请输入正确的' + message));
};

// 可传提示信息固定电话校验
export const phoneAndSolidifyCus = (rule, value, callback, message) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var pattern = /^1[3-9]\d{9}$/;
  var solidify = /^(0\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,6})?$/;
  if (pattern.test(value) || solidify.test(value)) {
    callback();
  }
  callback(new Error('请输入正确的' + message));
};

//不允许输入汉字
export const banWord = (rule, value, callback) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var pattern = /[\u4E00-\u9FA5]/g;
  if (pattern.test(value)) {
    callback(new Error('不能输入汉字!'));
  } else {
    callback();
  }
};
// 不允许输入空格
export const noSpace = (rule, value, callback) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var pattern = /\s+/;
  if (!pattern.test(value)) {
    callback();
  }
  callback(new Error('不允许输入空格'));
};
//获取当前日期
export const endTime = (rule, value, callback) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  // console.log(value,"有效期至")
  let publisDate1 = new Date(Date.parse(value));
  //搜索框时间
  let searchYear1 = publisDate1.getFullYear();
  let searchMonth1 = publisDate1.getMonth();
  let searchDate1 = publisDate1.getDate();
  // 当前时间
  let d = new Date();
  let nowYear1 = d.getFullYear();
  let nowMonth1 = d.getMonth() + 1 > 9 ? d.getMonth() + 1 : '0' + d.getMonth() + 1;
  let nowDate1 = d.getDate() > 9 ? d.getDate() : '0' + d.getDate();
  console.log(value, `${nowYear1}-${nowMonth1}-${nowDate1}`, 'nowYear1');
  if (value < `${nowYear1}-${nowMonth1}-${nowDate1}`) {
    callback(new Error('有效期至不允许选择过去日期'));
  }
};
export function doubleCheck(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^([1-9][0-9]*)+(\.[0-9]{1,2})?$/;
  if (g.test(value)) {
    callback();
  }
  callback(new Error('只能输入最多带两位小数的数字'));
}
export function number(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[0-9]*$/;
  if (g.test(value)) {
    callback();
  }
  callback(new Error('只能输入数字'));
}

export function numberCheck(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^\-?[0-9]\d*.?\d*$/;
  if (g.test(value)) {
    callback();
  }
  callback(new Error('只能输入数字'));
}
//0--100
export function numberCheckMax100(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^\-?[0-9]\d*.?\d*$/;
  if (g.test(value)) {
    if (100 >= value > 0) {
      callback();
    }
  }
  callback(new Error('只能输入数字，且在0-100之间'));
}
// 限制系统代码
export function systemCode(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\d|a-zA-Z|-]{1,}$/;
  if (!g.test(value)) {
    callback(new Error('代码只能为字母数字'));
  }
  callback();
}
// 限制车型代码
export function systemModeCode(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\d|a-zA-Z|-]{1,64}$/;
  if (!g.test(value)) {
    callback(new Error('代码只能为字母或数字，最多64字符'));
  }
  callback();
}
// 限制发票8位
export function invoice8(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\d|a-zA-Z|-]{8,8}$/;
  if (!g.test(value)) {
    callback(new Error('请输入数字或字母，8位'));
  }
  callback();
}
// 限制发票
export function invoice(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[0-9a-zA-Z]{12}$/;
  var g1 = /^[0-9]{12}$/;
  var g2 = /^[a-zA-Z]{12}$/;
  if (!g.test(value) || g1.test(value) || g2.test(value)) {
    callback(new Error('请输入数字和字母，12位'));
  }
  callback();
}
// 邮箱
export function email(rule, value, callback) {
  if (!value || value == null) {
    callback();
  }
  var g = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
  if (!g.test(value)) {
    callback(new Error('请输入有效的邮件地址'));
  }
  if (value) {
    if (value.length > 60) {
      callback(new Error('请输入有效的邮件地址'));
    }
  }
  callback();
}

// 转年月日时分秒
export function forDates(value) {
  // 这里的 value 就是需要过滤的数据
  if (value == null || value == '') {
    return '';
  }
  var date = new Date(Number(value));
  var year = date.getFullYear();
  var month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1);
  var day = date.getDate() + 1 > 9 ? date.getDate() + 1 : '0' + (date.getDate() + 1);
  // return year+"-"+month+"-"+day
  var hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
  var minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
  var second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
// 时间戳转年月日时分秒
export function formatDates(value) {
  // 这里的 value 就是需要过滤的数据
  if (value == null || value == '') {
    return '';
  }
  var date = new Date(Number(value));
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  // return year+"-"+month+"-"+day
  var hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
  var minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
  var second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
// 时间戳转年月日时分
export function formatDates_minute(value) {
  // 这里的 value 就是需要过滤的数据
  if (value == null || value == '') {
    return '';
  }
  var date = new Date(Number(value));
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  // return year+"-"+month+"-"+day
  var hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
  var minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
}
// 时间戳转年月日
export function formatDat(value) {
  // 这里的 value 就是需要过滤的数据
  if (value == null || value == '') {
    return '';
  }
  var date = new Date(Number(value));
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  return year + '-' + month + '-' + day;
}
// 时间戳转换天时分
export function formatDuring(mss) {
  var days = parseInt(mss / (1000 * 60 * 60 * 24));
  var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = (mss % (1000 * 60)) / 1000;
  return days + ' 天 ' + hours + ' 小时 ' + minutes + ' 分钟 ';
}

export function VIN(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback(new Error('请输入VIN'));
  }
  if (value.length !== 17) {
    callback(new Error('请输入正确的VIN!'));
  }
  var g = /^[\d|a-zA-Z|-]{17}$/;
  if (!g.test(value)) {
    callback(new Error('VIN由17位数字和字母组成!'));
  }
  callback();
}
// export const VIN = (rule, value, callback) => {
//   let reg = /^[a-zA-Z\d]{17}$/;
//   if (!reg.test(value)) {
//     callback(new Error('VIN由17位数字和字母组合'));
//   } else {
//     callback();
//   }
// };

// 行驶证只能13位
export function licenseNum(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\d|a-zA-Z|-]{13}$/;
  if (!g.test(value)) {
    callback(new Error('请输入13位行驶证号码!'));
  }
  callback();
}

export function basedataCode(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^(?![^A-Z]+$)(?!\D+$)/;
  if (!g.test(value)) {
    callback(new Error('长度6位:数字+大写字母组成'));
  }
  callback();
}

export const checkVehicleMark = (rule, value, callback) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var result = false;
  if (value != null && value.length == 7) {
    var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
    result = express.test(value);
  } else {
    var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-Z]{1}(([0-9]{5}[ADF])|([ADF][A-HJ-NP-Z0-9][0-9]{4}))$/;
    result = express.test(value);
  }
  if (result) {
    callback();
  } else {
    callback(new Error('车牌号不合法'));
  }
};

export function IDNumber(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  const city = {
    11: '北京',
    12: '天津',
    13: '河北',
    14: '山西',
    15: '内蒙古',
    21: '辽宁',
    22: '吉林',
    23: '黑龙江 ',
    31: '上海',
    32: '江苏',
    33: '浙江',
    34: '安徽',
    35: '福建',
    36: '江西',
    37: '山东',
    41: '河南',
    42: '湖北 ',
    43: '湖南',
    44: '广东',
    45: '广西',
    46: '海南',
    50: '重庆',
    51: '四川',
    52: '贵州',
    53: '云南',
    54: '西藏 ',
    61: '陕西',
    62: '甘肃',
    63: '青海',
    64: '宁夏',
    65: '新疆',
    71: '台湾',
    81: '香港',
    82: '澳门',
    91: '国外 '
  };

  // const tip = ''
  if (!value || !/^\d{15}$/ || !/^\d{6}(18|19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(value)) {
    callback(new Error('请输入正确的身份证号码'));
  } else if (!city[value.substr(0, 2)]) {
    callback(new Error('请输入正确的身份证号码'));
  } else {
    // 18位身份证需要验证最后一位校验位
    if (value.length === 18) {
      value = value.split('');
      // ∑(ai×Wi)(mod 11)
      // 加权因子
      var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
      // 校验位
      var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
      var sum = 0;
      var ai = 0;
      var wi = 0;
      for (var i = 0; i < 17; i++) {
        ai = value[i];
        wi = factor[i];
        sum += ai * wi;
      }
      // var last = parity[sum % 11]
      if (String(parity[sum % 11]) !== value[17]) {
        callback(new Error('请输入正确的身份证号码'));
      } else {
        callback();
      }
    } else {
      callback();
    }
  }
}
// 校验驾驶证
export function IDCard(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  const city = {
    11: '北京',
    12: '天津',
    13: '河北',
    14: '山西',
    15: '内蒙古',
    21: '辽宁',
    22: '吉林',
    23: '黑龙江 ',
    31: '上海',
    32: '江苏',
    33: '浙江',
    34: '安徽',
    35: '福建',
    36: '江西',
    37: '山东',
    41: '河南',
    42: '湖北 ',
    43: '湖南',
    44: '广东',
    45: '广西',
    46: '海南',
    50: '重庆',
    51: '四川',
    52: '贵州',
    53: '云南',
    54: '西藏 ',
    61: '陕西',
    62: '甘肃',
    63: '青海',
    64: '宁夏',
    65: '新疆',
    71: '台湾',
    81: '香港',
    82: '澳门',
    91: '国外 '
  };

  // const tip = ''
  if (!value || !/^\d{15}$/ || !/^\d{6}(18|19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(value)) {
    callback(new Error('请输入正确的证件号码'));
  } else if (!city[value.substr(0, 2)]) {
    callback(new Error('请输入正确的证件号码'));
  } else {
    // 18位证件号码需要验证最后一位校验位
    if (value.length === 18) {
      value = value.split('');
      // ∑(ai×Wi)(mod 11)
      // 加权因子
      var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
      // 校验位
      var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
      var sum = 0;
      var ai = 0;
      var wi = 0;
      for (var i = 0; i < 17; i++) {
        ai = value[i];
        wi = factor[i];
        sum += ai * wi;
      }
      // var last = parity[sum % 11]
      if (String(parity[sum % 11]) !== value[17]) {
        callback(new Error('请输入正确的证件号码'));
      } else {
        callback();
      }
    } else {
      callback();
    }
  }
}
export function CheckSocialCreditCode(Code) {
  var patrn = /^[0-9A-Z]+$/;
  //18位校验及大写校验
  if (Code.length != 18 || patrn.test(Code) == false) {
    console.info('不是有效的统一社会信用编码！');
    return false;
  } else {
    var Ancode; //统一社会信用代码的每一个值
    var Ancodevalue; //统一社会信用代码每一个值的权重
    var total = 0;
    var weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]; //加权因子
    var str = '0123456789ABCDEFGHJKLMNPQRTUWXY';
    //不用I、O、S、V、Z
    for (var i = 0; i < Code.length - 1; i++) {
      Ancode = Code.substring(i, i + 1);
      Ancodevalue = str.indexOf(Ancode);
      total = total + Ancodevalue * weightedfactors[i];
      //权重与加权因子相乘之和
    }
    var logiccheckcode = 31 - (total % 31);
    if (logiccheckcode == 31) {
      logiccheckcode = 0;
    }
    var Str = '0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y';
    var Array_Str = Str.split(',');
    logiccheckcode = Array_Str[logiccheckcode];

    var checkcode = Code.substring(17, 18);
    if (logiccheckcode != checkcode) {
      console.info('不是有效的统一社会信用编码！');
      return false;
    } else {
      console.info('yes');
    }
    return true;
  }
}

//验证护照号码
export function checkPassport(rule, value, callback) {
  var tip = 'OK';
  var pass = true;
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  if (!value || !/^((1[45]\d{7})|(G\d{8})|(P\d{7})|(S\d{7,8}))?$/.test(value)) {
    callback(new Error('请输入正确的护照号码'));
    // tip = "护照号码格式错误";
    // pass = false;
  }
  // return {'errCode':pass, 'errMsg':tip};
  callback();
}

//字母数字组合
export function testNumLetter(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  //纯数字/^([\u4e00-\u9fa5]|[0-9_a-zA-Z]){1,1000}$/
  var g = /^([0-9]){1,16}$/;
  //字母数字组合
  var g1 = /^([0-9_a-zA-Z]).{1,16}$/;
  //纯字母
  // var g2 = /^(?![^a-zA-Z]+$).{0,16}$/
  var g2 = /^[A-Za-z]{1,16}/;
  if (g.test(value) || g1.test(value) || g2.test(value)) {
    callback();
  } else {
    callback(new Error('字母或数字，最多16字符'));
  }
}
//字母数字组合
export function testNumLetter8(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  //纯数字/^([\u4e00-\u9fa5]|[0-9_a-zA-Z]){1,1000}$/
  var g = /^([0-9]){1,8}$/;
  //字母数字组合
  var g1 = /^([0-9_a-zA-Z]).{1,8}$/;
  //纯字母
  // var g2 = /^(?![^a-zA-Z]+$).{0,16}$/
  var g2 = /^[A-Za-z]{1,8}/;
  if (g.test(value) || g1.test(value) || g2.test(value)) {
    callback();
  } else {
    callback(new Error('字母或数字，最多8字符'));
  }
}
export function testNumLetter64(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  //纯数字/^([\u4e00-\u9fa5]|[0-9_a-zA-Z]){1,1000}$/
  var g = /^([0-9]){1,64}$/;
  //字母数字组合
  var g1 = /^([0-9_a-zA-Z]).{1,64}$/;
  //纯字母
  // var g2 = /^(?![^a-zA-Z]+$).{0,16}$/
  var g2 = /^[A-Za-z]{1,64}/;
  if (g.test(value) || g1.test(value) || g2.test(value)) {
    callback();
  } else {
    callback(new Error('字母或数字，最多64字符'));
  }
}
//纯文本
export function testText(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\u4E00-\u9FA5]{0,32}$/;
  if (!g.test(value)) {
    callback(new Error('纯文本内容，最多32字符'));
  }
  callback();
}

export function testText16(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\u4E00-\u9FA5]{0,16}$/;
  if (!g.test(value)) {
    callback(new Error('纯文本内容，最多16字符'));
  }
  callback();
}

export function testText32(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\u4E00-\u9FA5]{0,32}$/;
  if (!g.test(value)) {
    callback(new Error('纯文本内容，最多32字符'));
  }
  callback();
}

export function testText64(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\u4E00-\u9FA5]{0,64}$/;
  if (!g.test(value)) {
    callback(new Error('纯文本内容，最多64字符'));
  }
  callback();
}
//传真
export function testFax(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\u4E00-\u9FA5]{0,32}$/;
  if (g.test(value)) {
    callback(new Error('纯文本内容，最多32字符'));
  }
  callback();
}

export function checkDiscount(rule, value, callback) {
  const age = /^[0-9]+([.]{1}[0-9]+){0,1}$/;
  if (!age.test(value)) {
    callback(new Error('格式不正确'));
  } else {
    if (value > 0 && value <= 1) {
      callback();
    } else {
      callback(new Error('格式不正确'));
    }
  }
}

// 获取前后几个月的日期 month等于1就是返回下个月当天的日期格式
export function forMonth(month) {
  var time = new Date();
  time.setDate(time.getDate()); //获取Day天后的日期
  var y = time.getFullYear();
  var m;
  if (time.getMonth() + month + 1 > 12) {
    y = y + 1;
    m = time.getMonth() + month - 11; //获取当前月份的日期 d
  } else {
    m = time.getMonth() + month + 1; //获取当前月份的日期 d
  }
  var d = time.getDate();
  return y + '-' + m + '-' + d;
}

//密码验证
export function testPW(rule, value, callback) {
  // const g = /^(?!\d+$)(?![a-z]+$)(?![A-Z]+$)(?![^(a-z|A-Z|\d|\u4E00-\u9FA5)]+$).{6,32}$/
  //数字，大小写字母，特殊字符
  const g1 = /^.*(?=.{6,32})(?=.*\d)(?=.*[A-Z]{1,})(?=.*[a-z]{1,})(?=.*[!@#$%^&*?.\(\)]).*$/;
  //数字，大写字母，特殊字符
  const g2 = /^.*(?=.{6,32})(?=.*\d)(?=.*[A-Z]{1,})(?=.*[!@#$%^&*?.\(\)]).*$/;
  //数字，小写字母，特殊字符
  const g3 = /^.*(?=.{6,32})(?=.*\d)(?=.*[a-z]{1,})(?=.*[!@#$%^&*?.\(\)]).*$/;
  //数字，大，小写字母
  const g4 = /^.*(?=.{6,32})(?=.*\d)(?=.*[A-Z]{1,})(?=.*[a-z]{1,}).*$/;
  //大，小写字母，特殊字符
  const g5 = /^.*(?=.{6,32})(?=.*[A-Z]{1,})(?=.*[a-z]{1,})(?=.*[!@#$%^&*?.\(\)]).*$/;
  if (g1.test(value) || g2.test(value) || g3.test(value) || g4.test(value) || g5.test(value)) {
    callback();
  } else {
    callback(new Error('数字，大，小写字母，特殊字符中的3种'));
  }
}

// 邮箱校验
export const EmailValidate = (rule, value, callback) => {
  if (rule.required && isFormEmpty(value)) {
    return callback(new Error('邮箱不能为空'));
  }
  let regExp = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
  if (isFormEmpty(value) || regExp.test(value)) {
    return callback();
  }
  callback(new Error('邮箱格式不正确'));
};

// 邮编校验
export function zipCode(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    return callback();
  }
  let g = /^[0-9][0-9]{5}$/;
  if (!g.test(value)) {
    callback(new Error('请输入正确邮编'));
  }
  callback();
}

// 电话号校验
export function telePhone(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    return callback();
  }
  let g = /^0\d{2,3}-?\d{7,8}$/;
  if (!g.test(value)) {
    return callback(new Error('请输入正确的电话号码!'));
  }
  callback();
}

export function PHONE_PATTERN() {}

// 校验数据格式是否为数字（交车结算）
export const isValidateDoubleForFeesSettlement = (rule, value, callback) => {
  if (value != null && value != '') {
    if (!Number(value) && value != 0) {
      callback(new Error('请输入正确的数字格式!'));
    }
  }
  callback();
};

export function checkDiscountForFeesSettlement(rule, value, callback) {
  const age = /^[0-9]+([.]{1}[0-9]+){0,1}$/;
  if (!age.test(value)) {
    callback(new Error('格式不正确'));
  } else {
    if (value >= 0 && value <= 1) {
      return callback();
    } else {
      callback(new Error('格式不正确'));
    }
  }
}

// 产品代码校验
export const prodCode = (rule, value, callback) => {
  // if (rule.required && isFormEmpty(value)) {
  //   return callback(new Error('产品代码不能为空'));
  // }
  if (value === undefined || value === '' || value === null) {
    return callback();
  }
  let reg = /[A-Z0-9\s]{18}/;
  if (reg.test(value)) {
    return callback();
  } else {
    callback(new Error('产品代码格式不正确'));
  }
};
// 正数校验
// 正数校验
export const plus = (rule, value, callback, message) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^\-?[0-9]\d*.?\d*$/;
  if (g.test(value)) {
    callback();
  }
  callback(new Error('只能输入数字'));
  var pattern = /^([1-9]\d*((.\d+)*))|(0.\d+)$/;
  if (pattern.test(value)) {
    callback();
  }

  callback(new Error('请输入大于0的正数'));
};

export const partCodeP = (rule, value, callback) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var pattern =  /^[a-zA-Z0-9_]{0,}$/;
  if (pattern.test(value)) {
    callback();
  }
  callback(new Error('不能输入汉字和特殊符号'));
};

export function salesCode8(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\d]{8,8}$/;
  if (!g.test(value)) {
    callback(new Error('经销商代码请输入数字，8位'));
  }
  callback();
}

export function serviceCode9(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
 let g = /^[\d]{8,8}$/;
  if (!g.test(value)) {
    callback(new Error('服务站代码请输入数字，8位'));
  }
  callback();
}

export function companyCode10(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\d|a-zA-Z]{1,10}$/;
  if (!g.test(value)) {
    callback(new Error('代码只能为字母或数字，最多10字符'));
  }
  callback();
}

export function timeHourAndMin(rule, value, callback) {
  if (value === undefined || !Array.isArray(value) || value.length != 2) {
    callback();
  }
  var g = /^(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$/;
  if (!g.test(value[0]) || !g.test(value[1])) {
    callback(new Error('时间错误，请填写hh:mm格式'));
  }
  callback();
}


export function afterCurrentTime(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  let regex = /^[1-2][0-9][0-9][0-9]-([1][0-2]|0?[1-9])-([12][0-9]|3[01]|0?[1-9]) ([01]?[0-9]|[2][0-3]):([0-5][0-9])$/;
  let regex2 = /^[1-2][0-9][0-9][0-9]-([1][0-2]|0?[1-9])-([12][0-9]|3[01]|0?[1-9]) ([01]?[0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$/;
  let regex3 = /^[1-2][0-9][0-9][0-9]-([1][0-2]|0?[1-9])-([12][0-9]|3[01]|0?[1-9]) ([01]?[0-9]|[2][0-3])$/;
  if (regex.test(value)) {
    value = value + ":00";
  }else if(regex2.test(value)){
    value = value;
  }else if(regex3.test(value)){
    value = value + ":00:00";
  }else{
    value = value;
  }

  if(new Date(value) < new Date()){
    callback(new Error('所选时间不能小于当前时间'));
  }
  callback();
}



//正整数校验
export const positiveInteger = (rule, value, callback, message) => {
  console.log("value",value)
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var pattern = /^[+]{0,1}(\d+)$/;
  if (pattern.test(value)) {
    callback();
  }
  callback(new Error('请输入正整数'));
};

//可输入 8 位以下字母或数字
export function labourCode8(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^[\d|a-zA-Z|-]{1,8}$/;
  if (!g.test(value)) {
    callback(new Error('请输入8位以下字母或数字'));
  }
  callback();
}

export function includeZeroDouble(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^([0-9][0-9]*)+(\.[0-9]{1,2})?$/;
  if (g.test(value)) {
    callback();
  }
  callback(new Error('只能输入最多带两位小数的数字'));
}

//电话号码新
export const phoneAndSolidifyNew = (rule, value, callback) => {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var pattern = /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})(-?)(\d{4,8})|(\d{4}|\d{3})(-?)(\d{4,8})(-?)(\d{4}|\d{3}|\d{2}|\d{1})|(\d{4,8})(-?)(\d{4}|\d{3}|\d{2}|\d{1}))$)/;
  if (pattern.test(value)) {
    callback();
  }
  callback(new Error('请输入正确的手机号码或固话'));
};
export function amountCheck(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^\-?([0-9][0-9]*)+(\.[0-9]{1,2})?$/;
  if (g.test(value)) {
    callback();
  }
  callback(new Error('只能输入最多带两位小数的数字'));
}

export function excludeZeroDouble(rule, value, callback) {
  if (value === undefined || value === '' || value === null) {
    callback();
  }
  var g = /^([1-9]\d{0,9}(\.\d{1,2})?|([0](\.([0][1-9]|[1-9]\d{0,1}))))$/;
  if (g.test(value)) {
    callback();
  }
  callback(new Error('只能输入最多带两位小数的数字'));
}
